package com.yehuishou.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Classify;
import com.yehuishou.pojo.Configurations;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.Item;
import com.yehuishou.pojo.ItemClassify;
import com.yehuishou.pojo.Models;
import com.yehuishou.pojo.SelItemParameter;
import com.yehuishou.service.SellItemService;

/**
 * 
 * @author 杨斌
 *
 */

@Controller
public class SellItemController {
	@Resource
	private SellItemService sellitemservice;

	// 所有商品分页展示
	@RequestMapping("content-1_2")
	public String itemsel(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "10") Integer pageSize, SelItemParameter sip,
			HttpServletRequest request) {
		PageInfo<ItemClassify> pageinfo = sellitemservice.itemclassifysel(pageNum, pageSize, sip);
		List<Classify> list = sellitemservice.selclassify();
		request.setAttribute("pageinfo", pageinfo);
		request.setAttribute("list", list);
		request.setAttribute("sip", sip);
		return "content-1_2";
	}

	// 商品详情
	@RequestMapping("itemimg")
	public String itemimg(Integer i_id, HttpServletRequest request) {
		Map<String, Object> map = sellitemservice.itemsel(i_id);
		request.setAttribute("map", map);
		return "item-configurations";
	}
	
	
	// 商品删除
	@ResponseBody
	@RequestMapping("deleitem")
	public int itemdele(Integer i_id, Integer status) {
		if (status == 1) {
			return 1;
		} else {
			int i = sellitemservice.itemdele(i_id);
			if (i > 0) {
				return 2;
			} else {
				return 3;
			}
		}
	}

	// 商品添加跳转
	@RequestMapping("item-add")
	public String itemaddtz(HttpServletRequest request) {
		List<Classify> classify = sellitemservice.selclassify();
		Integer cid = classify.get(0).getCid();
		List<Brand> brand = sellitemservice.selbrand(cid);
		Integer pid = brand.get(0).getPid();
		List<Models> model = sellitemservice.selmodel(pid);
		request.setAttribute("classify", classify);
		request.setAttribute("brand", brand);
		request.setAttribute("model", model);
		return "item-add";
	}

	// 商品分类联动
	@ResponseBody
	@RequestMapping("getclassifyid")
	public Map<String, Object> itemaddtzld(Integer cid) {
		List<Brand> brand = sellitemservice.selbrand(cid);
		List<Models> model = null;
		if (brand.size() > 0) {
			Integer pid = brand.get(0).getPid();
			model = sellitemservice.selmodel(pid);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("brand", brand);
		map.put("model", model);
		return map;
	}
	// 商品分类联动
	@ResponseBody
	@RequestMapping("getbrandid")
	public Map<String, Object> itemaddtzld1(Integer pid) {
		List<Models> model = sellitemservice.selmodel(pid);
		Map<String, Object> map = new HashMap<>();
		map.put("model", model);
		return map;
	}

	// 商品title验证
	@ResponseBody
	@RequestMapping("checktitle")
	public boolean checktitle(String title, Integer cid, Integer pid, Integer mid) {
		int i = sellitemservice.checktitle(title, cid, pid, mid);
		if (i > 0) {
			return true;
		}
		return false;
	}

	// 商品添加
	@RequestMapping("itemadd")
	public String itemadd(Item item,String[] colormemory,double[] price,Integer[] stocknum,HttpServletRequest request) {
		Date d = new Date();
		item.setCreated(d);
		item.setUpdated(d);
		double lowprice=price[0];
		for (int i = 0; i < price.length; i++) {
			double pr=price[i];
			if(pr<lowprice){
				lowprice=pr;
			}
		}
		item.setLowprice(lowprice);
		int i=sellitemservice.itemadd(item);
		
		Integer i_id=item.getI_id();
		Configurations con=new Configurations();
		for (int j = 0; j < colormemory.length; j++) {
			String co=colormemory[j];
			double pr=price[j];			
			Integer st=stocknum[j];
			con.setI_id(i_id);
			con.setColormemory(co);
			con.setPrice(pr);
			con.setStocknum(st);
			int c=sellitemservice.configurationsadd(con);			
		}
		request.setAttribute("i_id", item.getI_id());
		return "item_add_imageparticulars";
	}
	//商品详情图片添加
	@RequestMapping("add_imageparticulars")
	public String add_imageparticulars(Item item){
		int i=sellitemservice.imageparticularsadd(item);
		return "redirect:content-1_2";
	}

	// 商品修改显示
	@RequestMapping("itemupdatetz")
	public String itemupdate(Integer i_id,Integer cid,Integer pid, HttpServletRequest request) {
		Map<String, Object> map = sellitemservice.itemsel(i_id);
		List<Classify> classify = sellitemservice.selclassify();
		List<Brand> brand = sellitemservice.selbrand(cid);
		List<Models> model = sellitemservice.selmodel(pid);
		request.setAttribute("map", map);
		request.setAttribute("classify", classify);
		request.setAttribute("brand", brand);
		request.setAttribute("model", model);
		return "item-update";
	}

	// 商品图片修改
	@ResponseBody
	@RequestMapping("imgupdate")
	public Map<String, Object> imgupdate(@RequestParam(defaultValue = "public") String model, @RequestParam String ext,
			@RequestParam MultipartFile file) {
		Map<String, Object> map = new HashMap<>();
		String rootPath = "/upload/";
		// 组装文件存储路径
		String filePath = rootPath + model + File.separator;

		// 创建目录
		File f = new File(filePath);
		if (!f.exists()) {
			f.mkdirs();
		}

		// 2.生成新的文件名
		String newFileName = UUID.randomUUID().toString() + ext;

		try {
			// 把上传的文件存储指定位置
			file.transferTo(new File(f, newFileName));
			// 文件上传操作
			String filepath = rootPath + model + File.separator + newFileName;
			String path = filepath.replace("\\", "/");
			ImageFile imgfile = new ImageFile();
			imgfile.setFilePath(path);
			int i = sellitemservice.save(imgfile);
			if (i > 0) {
				int id = imgfile.getFileId();
				String imgid = ";" + Integer.toString(id);
				map.put("imgid", imgid);
				map.put("filepath", filepath);
				return map;
			}
			return map;
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return map;

	}
	
	//商品配置删除
	@ResponseBody
	@RequestMapping("confdele")
	public int confdele(Configurations conf){
		int i=sellitemservice.confdele(conf);
		return i;
	}

	// 商品修改
	@RequestMapping("itemupdate")
	public String itemupdate(Item item,Integer[] cfid,String[] colormemory,double[] price,Integer[] stocknum,HttpServletRequest request) {
		Date d = new Date();
		item.setUpdated(d);
		Integer i_id=item.getI_id();
		double lowprice=price[0];
		for (int i = 0; i < price.length; i++) {
			double pr=price[i];
			if(pr<lowprice){
				lowprice=pr;
			}
		}
		item.setLowprice(lowprice);
		int i = sellitemservice.itemupdate(item);
		Configurations conf=new Configurations();
		int count=0;
		if(cfid!=null){
			count=cfid.length;
			if(count!=0){
				for (int j = 0; j < cfid.length; j++) {
					Integer cf=cfid[j];
					String co=colormemory[j];
					double pr=price[j];
					Integer st=stocknum[j];
					conf.setCfid(cf);
					conf.setColormemory(co);
					conf.setPrice(pr);
					conf.setStocknum(st);
					int c=sellitemservice.configurationsupdate(conf);
				}
			}
		}

		if(colormemory!=null){
			if(colormemory.length>count){
				for (int j = count; j < colormemory.length; j++) {
					String co=colormemory[j];
					double pr=price[j];
					Integer st=stocknum[j];
					conf.setColormemory(co);
					conf.setPrice(pr);
					conf.setStocknum(st);
					conf.setI_id(i_id);
					int p=sellitemservice.configurationsadd(conf);
				}
			}
		}
		Map<String, Object> map = sellitemservice.itemsel(i_id);
		request.setAttribute("map", map);
		return "item_update_imageparticulars";
	}
	
	// 商品修改底部详情图片显示
	@RequestMapping("update_imageparticulars")
	public String update_imageparticulars(Item item, HttpServletRequest request) {
		int i=sellitemservice.imageparticularsupdate(item);
		return "redirect:content-1_2";
	}
	
	//批量删除商品
	@ResponseBody
	@RequestMapping("deleItems")
	public boolean deleItems(@RequestParam(value="i_id[]")Integer[] i_id, @RequestParam(value="status[]")Integer[] status, HttpServletRequest request) {
		Integer j=0;
		for (int i = 0; i < status.length; i++) {
			if(status[i]==2) {
				sellitemservice.itemdele(i_id[i]);
			}else {
				j++;
			}
		}
		if(j>0) {
			return false;
		}
		return true;
	}

}
